/*
 * @Author: licheng
 * @Date: 2024-01-22 17:54:00
 * @LastEditors: licheng
 * @LastEditTime: 2024-02-01 16:45:14
 * @Descripttion: 要素图层方法
 */

const addWFSLayer = () => {}

/**
 * 初始化地图样式
 */
const addBaseMap = () => {
  //添加掩膜掩膜
  var bdary = new (window as any).BMapGL.Boundary()
  bdary.get('广东省广州市', function (rs: any) {
    // 绘制行政区
    for (var i = 0; i < rs.boundaries.length; i++) {
      var xyArr = rs.boundaries[i].split(';')
      var ptArr = []
      for (var j = 0; j < xyArr.length; j++) {
        var tmp = xyArr[j].split(',')
        var pt = new (window as any).BMapGL.Point(tmp[0], tmp[1])
        ptArr.push(pt)
      }
      var mapmask = new (window as any).BMapGL.MapMask(ptArr, {
        isBuildingMask: true,
        isPoiMask: true,
        isMapMask: true,
        showRegion: 'inside'
      })
      mapmask.layerName = '区域掩膜'
      ;(window as any).map.addOverlay(mapmask)
    }
  })

  //添加地图边界线、行政区边界线、3D棱柱边界
  let xzqhArr = [
    '广东省广州市天河区',
    '广东省广州市黄埔区',
    '广东省广州市越秀区',
    '广东省广州市海珠区',
    '广东省广州市荔湾区',
    '广东省广州市白云区',
    '广东省广州市番禺区',
    '广东省广州市增城区',
    '广东省广州市南沙区',
    '广东省广州市从化区',
    '广东省广州市花都区'
  ]
  xzqhArr.forEach((x: any) => {
    bdary.get(x, function (rs: any) {
      createBundleRenderer(rs)
    })
  })

  function createBundleRenderer(rs: any) {
    let count = rs.boundaries.length
    for (let i = 0; i < count; i++) {
      let path = []
      let str = rs.boundaries[i].replace(' ', '')
      let points = str.split(';')
      for (let j = 0; j < points.length; j++) {
        let lng = points[j].split(',')[0]
        let lat = points[j].split(',')[1]
        path.push(new (window as any).BMapGL.Point(lng, lat))
      }
      let prism = new (window as any).BMapGL.Prism(path, 5000, {
        topFillColor: '#5679ea',
        topFillOpacity: 0.3,
        sideFillColor: '#5679ea',
        sideFillOpacity: 0.9
      })
      prism.layerName = '3D棱柱'
      ;(window as any).map.addOverlay(prism)

      var border = new (window as any).BMapGL.Polyline(path, {
        strokeColor: '#4ca7a2',
        strokeWeight: 3,
        strokeOpacity: 1
      })
      border.layerName = '行政区划边界线'
      ;(window as any).map.addOverlay(border)
    }
  }

  // 监听地图层级
  ;(window as any).map.addEventListener('zoomend', function () {
    var ZoomNum = (window as any).map.getZoom()
    let all = (window as any).map.getOverlays()
    if (ZoomNum > 13) {
      all.map((item: any) => {
        if (item.layerName === '3D棱柱') {
          item.hide()
        }
      })
    } else {
      all.map((item: any) => {
        if (item.layerName === '3D棱柱') {
          item.show()
        }
      })
    }
  })
}

const addPoint = (objs: any) => {
  var myIcon = new (window as any).BMapGL.Icon(
    objs.imagesUrl,
    new (window as any).BMapGL.Size(objs.imageSize[0], objs.imageSize[1])
  )
  objs.pointArr.forEach((item: any) => {
    var pt = new (window as any).BMapGL.Point(item.x, item.y)
    var marker = new (window as any).BMapGL.Marker(pt, {
      icon: myIcon
    })
    marker.layerName = objs.layerName
    marker.name = item.name
    marker.content = item.content
    ;(window as any).map.addOverlay(marker)
    if (objs.pointArr.length == 1) {
      ;(window as any).map.centerAndZoom(pt, 15)
    }
    marker.addEventListener('click', function (e: any) {
      console.log('点击地图打点事件', e)
    })
  })
}

/**
 * 清除单个类型点
 * @param layerName 图层名称
 */
const removePoint = (layerName: any) => {
  for (let i in (window as any).map._overlays) {
    let item = (window as any).map._overlays[i]
    if (item.layerName == layerName) {
      ;(window as any).map.removeOverlay(item)
    }
  }
}

/**
 * 清楚所有图层只保留初始效果
 */
const removeAllOverlay = () => {
  let reserveLayer = ['区域掩膜', '3D棱柱', '行政区划边界线']
  for (let i in (window as any).map._overlays) {
    let item = (window as any).map._overlays[i]
    if (!reserveLayer.includes(item.layerName)) {
      ;(window as any).map.removeOverlay(item)
    }
  }
}

export { addWFSLayer, addBaseMap, addPoint, removePoint, removeAllOverlay }
